#!/usr/bin/env groovy

/**
# 前端基础打包脚本
# Author yousong.xiang
# Date 2022.6.22
# v1.0.2
# 规范：1,前端项目npm构建集成在Dockerfile文件中 2,dockerfile文件对应不同环境，如Dockerfile.test
# 仓库标准信息参考：https://gitwbp5.tostar.top/Bailun/brokers-view-web
# 源文件可参考deploy_front_base.groovy

**/

/**
# 参数信息：
@Library('ops-pipline-library')_

// @Library('xiangys0134_test')_
def map = [:]
    // 自定义构建运行node节点环境,根据实际情况进行调整
    map.put('JENKINS_NODE','brokersview')
    // 应用的名称，最好与helm定义的应用一致
    map.put('APP_NAME','brokersview-web')
    map.put('DOCKER_REGISTRY','https://registry.cn-shenzhen.aliyuncs.com')
    map.put('DOCKER_IMG','registry.cn-shenzhen.aliyuncs.com/stl-test/brokersview')
    map.put('DOCKER_REGISTRY_KEY','fb805ed9-7e7e-4c79-a9e4-325d5afacc7a')
    map.put('GIT_URL','ssh://git@gitwbp5.tostar.top/Bailun/brokers-view-web.git')
    map.put('GIT_KEY','92b42e30-38f2-442d-a2c2-9763fb14d6bf')
    map.put('DEPLOY_JENKINS_JOB','yunwei_update_test')
    map.put('DEPLOY_MAP','release_dev:yunwei_update_test,release_test:v2')


deploy_front_npm_base(map)
**/

def call(Map map) {
  pipeline {
    agent {
      node {
        label map.JENKINS_NODE
      }
    }

    environment {
      // sDate=time()
      app_name="${map.APP_NAME}"
      // dockerfile="Dockerfile.${PROFILE}"
      dockerfile="${map.DOCKERFILE_NAME}"
      dockerdir='./'
      // dockerregistry='https://553972031073.dkr.ecr.ap-east-1.amazonaws.com'
      dockerregistry="${map.DOCKER_REGISTRY}"
      docker_image="${map.DOCKER_IMG}"
      startDate=timeForm()

      data_agent=dataAgent()
      envtag=imageTag()

      docker_image_tag="${env.app_name}-${env.envtag}"
      docker_key="${map.DOCKER_REGISTRY_KEY}"
      parent_git_url="${map.PARENT_GIT_URL}"
      git_url="${map.GIT_URL}"
      git_key="${map.GIT_KEY}"
      // 父项目仓库分支
      dev_parent_tag=varEnv.branchTag(PARENT_BRANCH)
      dev_tag=varEnv.branchTag(BRANCH)
      // 父项目克隆目录
      parent_project='parent_directory'
      project='code_directory'
      deploy_map="${map.DEPLOY_MAP}"
      deploy_update_job="${map.DEPLOY_JENKINS_JOB}"
      jobs = varEnv.updateJobList(JOB_UPDATE_LIST,deploy_map)
      // 父项目构建命令
      parent_build_cmd="${map.PARENT_BUILD_CMD}"
      build_cmd="${map.BUILD_CMD}"
      build_agrs="${map.DOCKER_BUILD_ARGS}"
      build_dir_check=varEnv.buildDir("${map.BUILD_DIR}")
      docker_build_dir="${env.WORKSPACE}/${env.project}/${env.build_dir_check}"
    }
    stages {
      stage('删除工作空间') {
        steps {
          script{
            deleteDir()
          }
        }
      }
      stage('拉取parent git仓库') {
        steps {
          sshagent(credentials: [git_key], ignoreMissing: true) {
            dir("${env.WORKSPACE}") {
              sh '''
                    git clone --depth 1 --branch=${dev_parent_tag} ${parent_git_url} ${WORKSPACE}/${parent_project}
                 '''
            }
          }
        }
      }
      stage('拉取git仓库') {
        steps {
          sshagent(credentials: [git_key], ignoreMissing: true) {
            dir("${env.WORKSPACE}") {
              sh '''
                    git clone --depth 1 --branch=${dev_tag} ${git_url} ${WORKSPACE}/${project}
                 '''
            }
          }
        }
      }
      stage('parent代码编译') {
        steps {
          script {
            dir("${env.parent_project}") {
              sh'''
                #执行shell命令
                if [ "${parent_build_cmd}" != "null" ]; then
                  echo "running exec: ${parent_build_cmd}"
                  sh -c "${parent_build_cmd}"
                fi

              '''
            }
          }
        }
      }
      stage('代码编译') {
        steps {
          script {
            dir("${env.docker_build_dir}") {
              sh'''
                #执行shell命令
                if [ "${build_cmd}" != "null" ]; then
                  echo "running exec: ${build_cmd}"
                  sh -c "${build_cmd}"
                fi

              '''
            }
          }
        }
      }
      stage('build构建镜像') {
        steps {
          dir("${env.docker_build_dir}"){
            script {
              try {
                withDockerRegistry(credentialsId: docker_key, url: dockerregistry) {
                  def customImage = docker.build("${docker_image}:${docker_image_tag}", "${build_agrs} ${dockerfile} ${dockerdir}")
                  customImage.push()
                }
              }
              catch(exc){
                currentBuild.result='FAILURE'
                echo'构建失败'
                sh'exit 4'
              }
            }
          }
        }
      }

      stage('关联发布') {
        when {
          allOf{
            expression {
              return env.jobs != 'null';
            }
          }
        }
        steps {
          script{
            for(item in jobs.tokenize(',')){
              build job: item, parameters: [
                      string(name: 'release_tag', value: env.docker_image_tag)
              ]
            }
          }
        }
      }
    }
  }

}
